<template>
  <div id="particles-js">
    <div class="login">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <!-- 广告发布平台 -->
        <h2 class="title">
          {{ $t('loginPage.release') }}
        </h2>
        <!-- 语言切换 -->
        <el-row class="languageToggle">
          <el-col :span="8"><br /></el-col>
          <el-col :span="8">
            <br />
          </el-col>
          <el-col :span="8" style="text-align:right">
            <el-dropdown
              trigger="click"
              hide-on-click
              @command="changeLanguage"
            >
              <span class="el-dropdown-link">
                {{ $t('loginPage.language')
                }}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <!-- 简体中文 -->
                <el-dropdown-item command="zh">
                  简体中文
                </el-dropdown-item>
                <!-- 英文 -->
                <el-dropdown-item command="en">English</el-dropdown-item>
                <!-- 繁体中文 -->
                <el-dropdown-item command="hk">
                  繁体中文
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
        <!-- uid -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            :placeholder="$t('loginPage.userName')"
            maxlength="18"
            clearable
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <!-- password -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            :placeholder="$t('loginPage.Password')"
            @keyup.enter.native="handleLogin"
            maxlength="18"
            show-password
            clearable
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            :placeholder="$t('loginPage.verificationCode')"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
            maxlength="6"
            clearable
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <!-- 记住密码 -->
        <el-checkbox
          v-model="loginForm.rememberMe"
          style="margin:0px 0px 25px 0px;"
          >{{ $t('loginPage.autologin') }}</el-checkbox
        >
        <!-- 登录按钮 -->
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">{{ $t('loginPage.SignIn') }}</span>
            <!-- 登 录 中... -->
            <span v-else>{{ $t('loginPage.loading') }}</span>
          </el-button>
        </el-form-item>
      </el-form>
      <!--  底部  -->
      <div class="el-login-footer">
        <span>Copyright © 2010-2021 eagle.vip All Rights Reserved.</span>x
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import i18n from '@/lang/index' // i8n
import particlesJs from 'particles.js'
import particlesConfig from '@/utils/animate/particles.json'
import { mapState, mapMutations } from 'vuex'
// var Mock = require('mockjs') // 使用mock.js
export default {
  name: 'Login',
  data () {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      i18n: i18n,
      // 效验
      loginRules: {
        // 用户名
        username: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(
                  new Error(this.$t('loginPage.TheUserNameCannotBeEmpty'))
                )
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        // 密码
        password: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(
                  new Error(this.$t('loginPage.ThePasswordCannotBeEmpty'))
                )
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        // 验证码
        code: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(
                  new Error(
                    this.$t('loginPage.TheVerificationCodeCannotBeEmpty')
                  )
                )
              }
              callback()
            },
            trigger: ['blur', 'change']
          }
        ]
      },
      loading: false,
      redirect: undefined,
      // 语言切换
      value: ''
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query.redirect
      },
      immediate: true
    },
    'i18n.locale': {
      handler (newVal, oldVal) {
        switch (newVal) {
          case 'zh':
            if (!this.loginForm.username.length) {
              this.$refs.loginForm.clearValidate('username')
            }
            if (!this.loginForm.password.length) {
              this.$refs.loginForm.clearValidate('password')
            }
            if (!`${this.loginForm.code || ''}`.length) {
              this.$refs.loginForm.clearValidate('code')
            }
            break
          case 'en':
            if (!this.loginForm.username.length) {
              this.$refs.loginForm.clearValidate('username')
            }
            if (!this.loginForm.password.length) {
              this.$refs.loginForm.clearValidate('password')
            }
            if (!`${this.loginForm.code || ''}`.length) {
              this.$refs.loginForm.clearValidate('code')
            }
            break
          case 'hk':
            if (!this.loginForm.username.length) {
              this.$refs.loginForm.clearValidate('username')
            }
            if (!this.loginForm.password.length) {
              this.$refs.loginForm.clearValidate('password')
            }
            if (!`${this.loginForm.code || ''}`.length) {
              this.$refs.loginForm.clearValidate('code')
            }
            break
          default:
            break
        }
      }
    }
  },
  created () {
    this.getCode()
    this.getCookie()
  },
  methods: {
    init () {
      particlesJS('particles-js', particlesConfig)
      document.body.style.overflow = 'hidden'
    },
    // 获取验证码
    getCode () {
      getCodeImg().then(res => {
        this.codeUrl = 'data:image/gif;base64,' + res.img
        this.loginForm.uuid = res.uuid
      })
    },
    // 获取cookie
    getCookie () {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    // form效验
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 30
            })
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 30
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
				let a=/^[0-9]{8,16}$/
				let b=/^[a-zA-Z]{8,16}$/
				if(this.loginForm.password==123456||a.test(this.loginForm.password)||b.test(this.loginForm.password)){
					this.$message({
						showClose: true,
						message: '当前登录密码强度较低，建议登录之后在用户管理页面或者个人信息页面进行密码修改',
						type: 'warning'
					});
				}
              // 效验成功跳转的路由
              // console.log(this.redirect)
              // this.$router.push({ path:'count/information' }).catch((err) => {
              this.$router.push({ path: this.redirect || '/' }).catch(() => {})
            })
            .catch(() => {
              this.loading = false
              this.getCode() //刷新验证码
              this.loginForm.code = '' // 清空文本框
            })
        }
      })
    },
    // 语言切换功能
    changeLanguage (command) {
      this.$i18n.locale = command
      window.localStorage.setItem('languageval', command)
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.title {
  margin: 10px auto 5px auto;
  text-align: center;
  color: #707070;
}
.login {
  z-index: 2;
  position: absolute;
  width: 350px;
  border-radius: 5px;
  // height: 500px;
  background: white;
  box-shadow: 0px 0px 5px #333333;
  top: 60%;
  left: 50%;
  margin-top: -250px;
  margin-left: -175px;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 1s;
  -moz-transition: all 1s; /* Firefox 4 */
  -webkit-transition: all 1s; /* Safari 和 Chrome */
  -o-transition: all 1s; /* Opera */
}
.login-top {
  font-size: 24px;
  margin-top: 100px;
  padding-left: 40px;
  box-sizing: border-box;
  color: #333333;
  margin-bottom: 50px;
}
.login-center {
  width: 100%;
  box-sizing: border-box;
  padding: 0 40px;
  margin-bottom: 30px;
}
.login-center-img {
  width: 20px;
  height: 20px;
  float: left;
  margin-top: 5px;
}
.login-center-img > img {
  width: 100%;
}
.login-center-input {
  float: left;
  width: 230px;
  margin-left: 15px;
  height: 30px;
  position: relative;
}
.login-center-input input {
  z-index: 2;
  transition: all 0.5s;
  padding-left: 10px;
  color: #333333;
  width: 100%;
  height: 30px;
  border: 0;
  border-bottom: 1px solid #cccccc;
  border-top: 1px solid #ffffff;
  border-left: 1px solid #ffffff;
  border-right: 1px solid #ffffff;
  box-sizing: border-box;
  outline: none;
  position: relative;
}
.login-center-input input:focus {
  border: 1px solid dodgerblue;
}
.login-center-input-text {
  background: white;
  padding: 0 5px;
  position: absolute;
  z-index: 0;
  opacity: 0;
  height: 20px;
  top: 50%;
  margin-top: -10px;
  font-size: 14px;
  left: 5px;
  color: dodgerblue;
  line-height: 20px;
  transition: all 0.5s;
  -moz-transition: all 0.5s; /* Firefox 4 */
  -webkit-transition: all 0.5s; /* Safari 和 Chrome */
  -o-transition: all 0.5s; /* Opera */
}
.login-center-input input:focus ~ .login-center-input-text {
  top: 0;
  z-index: 3;
  opacity: 1;
  margin-top: -15px;
}
.login.active {
  -webkit-animation: login-small 0.8s;
  animation: login-small 0.8s;
  animation-fill-mode: forwards;
  -webkit-animation-fill-mode: forwards;
}
.login-button {
  cursor: pointer;
  width: 250px;
  text-align: center;
  height: 40px;
  line-height: 40px;
  background-color: dodgerblue;
  border-radius: 5px;
  margin: 0 auto;
  margin-top: 50px;
  color: white;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  // width: 400px;
  // padding: 25px 25px 5px 25px;
  .languageToggle {
    padding-bottom: 10px;
    .el-dropdown-link {
      cursor: pointer;
    }
    .el-icon-arrow-down {
      font-size: 12px;
    }
  }
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

// background-image style
#particles-js {
  width: 100%;
  height: 100%;
  position: relative;
  background-image: url('~@/assets/images/bg.jpg'); // ../img/bg.jpg
  background-position: 50% 50%;
  background-size: cover;
  background-repeat: no-repeat;
  margin-left: auto;
  margin-right: auto;
}
</style>
